package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.aop.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;


@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {


    @Resource
    private SetMealMapper setMealMapper;

    @Resource
    private SetMealDishMapper setMealDishMapper;


    @Transactional(rollbackFor = SQLException.class)
    @Override
    public Long addSetMeal(SetmealDTO setmealDTO) {

        log.info("【新增套餐S】 dto：{}",setmealDTO);
        // 将dto 妆花为 实体类
        Dish dish = new Dish();
        BeanUtils.copyProperties(setmealDTO, dish);

        // 将数据插入到套餐表
        Long tag = setMealMapper.insertSetMeal(dish);

        // 将菜品表插入到套餐菜品表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.stream().forEach(setmealDish -> {
            setmealDish.setSetmealId(dish.getId());
//            setMealDishMapper.insertSetMealDish(setmealDish);
        });


        // 批量新增
        setMealDishMapper.insertBatchSetMealDish(setmealDishes);

        return tag;
    }

    @Override
    public PageResult findByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 配置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<SetmealVO> setmealVOS = setMealMapper.selectByPage(setmealPageQueryDTO);
        log.info("【套餐查询】vo：{}",setmealVOS);
        Page<SetmealVO> p = (Page<SetmealVO>) setmealVOS;
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public Integer startOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        return setMealMapper.updateById(setmeal);
    }

    @Override
    public SetmealVO findById(Long id) {
        Setmeal setmeal = setMealMapper.selectById(id);
        List<SetmealDish> setmealDish = setMealDishMapper.selectBySetmealId(id);

        //封装vo
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        // 将菜品添加到vo 对象中
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }


    @Transactional(rollbackFor = SQLException.class)
    @Override
    public Integer updateById(SetmealDTO setmealDTO) {

        // 将dto 转为 实体类
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        Integer tag = setMealMapper.updateById(setmeal);

        // 将套餐菜品删除后，重新添加
        setMealDishMapper.deleteBySetmealId(setmealDTO.getId());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.stream().forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            // 单条插入
//            setMealDishMapper.insertSetMealDish(setmealDish);
        });

        // 批量插入
        setMealDishMapper.insertBatchSetMealDish(setmealDishes);
        return tag;
    }

    @Transactional(rollbackFor = SQLException.class)
    @Override
    public Integer removeBatchByIds(List<Long> ids) {

        // 启售中的套餐不能删除
        ids.forEach(id -> {
            Setmeal setmeal = setMealMapper.selectById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        // yewu
        //删除套餐 ：用xml批量删除《foreachd》
        Integer tag = setMealMapper.deleteByIds(ids);
        // 删除套餐-菜品：循环删除的
        ids.stream().forEach(setmeal_id -> {
            setMealDishMapper.deleteBySetmealId(setmeal_id);
        });
        return tag;
    }



    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
